<?php
/**
 * Ajax for Admin Login Class - PHP5
 *
 * Description:
 * This class handles the functionality for users login via Ajax
 *
 * @copyright	Copyright (c) eFun PMS 10th March, 2010.
 * @version 	1.0.0
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 **/
class ajax extends Controller
{
	var $c 		= 'ajax';//current controler
	var $p_c 	= 'login';//parent controller
	var $ajax_arr = array();
	/** 
	 * Welcome
	 *
	 * @desc contructor of the class
	 * @access public
	 * @param  NA	 
	 * @return void
	 */
	function ajax()
	{
		parent::Controller();	
    
    $this->ajax_arr['suc'] = '';
    $this->ajax_arr['err'] = '';
    $this->ajax_arr['msg'] = '';
    $this->ajax_arr['act'] = '';
    
		//load login language
		$this->lang->load('login', ADMIN_LANGUAGE);    
    $this->load->model('DB_F_Model', 'db_model');
	}
	
	/* ------------------------------------------------------------------------- */
	
	/** 
	 * Index
	 *
	 * @desc NA
	 * @access public/private
	 * @param  NA	 
	 * @return void/mixed
	 */
	function index()
	{		
		exit;
	}
	
	/** 
	 * JAlerts
	 *
	 * @desc this will add the journal alerts
	 * @access public
	 * @param  NA	 
	 * @return mixed
	 */
	function jalerts()
	{
    if(__is_loggedin()){     
      $usr_info = $this->session->userdata('usr_info');	
      
      $aid = $this->input->post('aid');
      $qry = "SELECT j.os_journals_id as `id`, j.os_journals_title as `name`, j.os_journals_issn as `issn`
            FROM ". TBL_JOURNALS ."  j 
            WHERE j.os_journals_id = '".$aid."'";    
      $set = run_cust_qry($qry,TRUE);
      if($set['num_rows'] > 0){      
        
        $name = $set['list']['name'] .' '. $set['list']['issn'];
        
        $qry = "SELECT * FROM ". TBL_MEMBERS_ALERTS ." 
                WHERE os_journals_id = '".$aid."'";    
        $set = run_cust_qry($qry,TRUE);
        if($set['num_rows'] > 0){  
          $this->ajax_arr['err'] = 1;    
          $this->ajax_arr['msg'] = sprintf($this->lang->line('jalerts_exists_error'), $name);   
        }else{
          $data = array();
          $data['os_journals_id'] = $aid;
          $data['os_mem_id'] = $usr_info['usr_id'];
          $this->db->insert(TBL_MEMBERS_ALERTS, $data);           
          $this->ajax_arr['suc'] = 1;
          $this->ajax_arr['msg'] = sprintf($this->lang->line('jalerts_sucess'), $name);
        }
      }else{
        $this->ajax_arr['err'] = 1;    
        $this->ajax_arr['msg'] = $this->lang->line('jalerts_error');   
      }      
    }else{      
      $this->ajax_arr['err'] = 1;    
      $this->ajax_arr['msg'] = $this->lang->line('no_login_msg');      
    }  
    //$this->ajax_arr['act'] = "";    
    $this->_ajax_wrapper();
	}
  
  /** 
	 * JFav
	 *
	 * @desc this will add the journal favourites
	 * @access public
	 * @param  NA	 
	 * @return mixed
	 */
	function jfav()
	{
    if(__is_loggedin()){     
      $usr_info = $this->session->userdata('usr_info');	
      
      $aid = $this->input->post('aid');
      $qry = "SELECT j.os_journals_id as `id`, j.os_journals_title as `name`, j.os_journals_issn as `issn`
            FROM ". TBL_JOURNALS ."  j 
            WHERE j.os_journals_id = '".$aid."'";    
      $set = run_cust_qry($qry,TRUE);
      if($set['num_rows'] > 0){      
        
        $name = $set['list']['name'] .' '. $set['list']['issn'];
        
        $qry = "SELECT * FROM ". TBL_MEMBERS_FAV ." 
                WHERE os_journals_id = '".$aid."'";    
        $set = run_cust_qry($qry,TRUE);
        if($set['num_rows'] > 0){  
          $this->ajax_arr['err'] = 1;    
          $this->ajax_arr['msg'] = sprintf($this->lang->line('jfav_exists_error'), $name);   
        }else{
          $data = array();
          $data['os_journals_id'] = $aid;
          $data['os_mem_id'] = $usr_info['usr_id'];
          $this->db->insert(TBL_MEMBERS_FAV, $data);           
          $this->ajax_arr['suc'] = 1;
          $this->ajax_arr['msg'] = sprintf($this->lang->line('jfav_sucess'), $name);
        }
      }else{
        $this->ajax_arr['err'] = 1;    
        $this->ajax_arr['msg'] = $this->lang->line('jfav_error');   
      }      
    }else{      
      $this->ajax_arr['err'] = 1;    
      $this->ajax_arr['msg'] = $this->lang->line('no_login_msg');      
    }  
    //$this->ajax_arr['act'] = "";    
    $this->_ajax_wrapper();
	}
  
  
  /** 
	 * JFav
	 *
	 * @desc this will add the journal favourites
	 * @access public
	 * @param  NA	 
	 * @return mixed
	 */
	function stats($mode = '')
	{
    $mem_id = '';
    if(__is_loggedin()){     
      $usr_info = $this->session->userdata('usr_info');	
      $mem_id = $usr_info['usr_id'];
    }
    
    switch ($mode){
      default:
      break;
        
      case 'jour':
        $jid = $this->input->post('jid');
        $data = array();
        $data['os_mem_id']      = $mem_id;
        $data['os_journals_id'] = $jid;        
        $data['os_stats_most_read_created'] = date(DB_DATE_FORMAT);
        $this->db->insert(TBL_STATS_MOST_RD, $data); 
      break; 
    
      case 'art':
        $jid = $this->input->post('jid');
        $aid = $this->input->post('aid');
        $data = array();
        $data['os_mem_id']      = $mem_id;
        $data['os_journals_id'] = $jid;
        $data['os_articles_id'] =  $aid;
        $data['os_stats_most_read_created'] = date(DB_DATE_FORMAT);
        $this->db->insert(TBL_STATS_MOST_RD, $data); 
      break; 
    
      case 'pubrd':
        $pid = $this->input->post('pid');
        if(!empty ($pid)){
          $data = array();
          $data['os_mem_id'] = $mem_id;
          $data['os_pub_id'] = $pid;
          $data['os_stats_most_read_pub_created'] = date(DB_DATE_FORMAT);
          $this->db->insert(TBL_STATS_MOST_RD_PUB, $data); 
        }
      break; 
      
      case 'pubvis':
        $pid = $this->input->post('pid');
        if(!empty ($pid)){
          $data = array();
          $data['os_mem_id'] = $mem_id;
          $data['os_pub_id'] = $pid;
          $data['os_stats_most_vis_pub_created'] = date(DB_DATE_FORMAT);
          $this->db->insert(TBL_STATS_MOST_VIS_PUB, $data); 
        }
      break; 
    }
	}
  
  function suggen() {
    $var  = '';
    $search = $this->input->post('queryString');
    if(!empty($search)){
      $qry = "SELECT DISTINCT(os_journals_title) as resultset, os_journals_id as `id`
              FROM ".TBL_JOURNALS."
              WHERE os_journals_title ".filter_data($search,'search_like')." 
              AND os_journals_status = '1' 
              ORDER BY resultset
              LIMIT 4
              ";			
      //__debug($qry);exit ();
      $data = run_cust_qry($qry);
      if($data['num_rows'] > 0){
        $var .= '<ul>';
        foreach ($data['list'] as $key => $value) {
          $var .= '<li onClick="fill(\''.  $value['id'] .'\');">'.  addslashes(filter_data($value['resultset'],'display')).'</li>';
        }
				$var .= '</ul>';
      }
    }
    echo $var;
  }
  
  
	/** 
	 * Ajax Wrapper
	 *
	 * @descp This will send the arry in JSON format
	 * @access private
	 * @param  NA	 
	 * @return void
	 */
	function _ajax_wrapper()
	{		
		echo json_encode($this->ajax_arr);
	}
	
}

/* End of file welcome.php */
/* Location: ./system/application/modules/admin_login/controllers/ajax.php */
?>